package com.gwq.controller;


import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gwq.base.ResponseData;
import com.gwq.dto.SysMenuDto;
import com.gwq.entity.SysMenu;
import com.gwq.entity.SysRoleMenu;
import com.gwq.entity.SysUser;
import com.gwq.service.*;
import com.gwq.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  菜单管理
 * </p>
 *
 * @author gwq
 * @since 2022-06-07
 */
@Api(description = "菜单")
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController {

    @ApiOperation(value="获取菜单信息")
    @GetMapping("/nav")
    public ResponseData nav(Principal principal){
        SysUser sysUser = sysUserService.getByUserName(principal.getName());
        // 获取权限信息
        String userAuthorityInfo = sysUserService.getUserAuthorityInfo(sysUser.getId());
        String[] tokenizeToStringArray = StringUtils.tokenizeToStringArray(userAuthorityInfo, ",");

        // 获取导航栏信息
        List<SysMenuDto> navs = sysMenuService.getCurrentUserNav();

        return ResponseData.success(MapUtil.builder().put("authoritys", tokenizeToStringArray).put("nav", navs).map());
    }

    @ApiOperation(value="根据id查询")
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAnyAuthority('sys:menu:list')")
    public ResponseData info(@PathVariable(name = "id") String id){
        return ResponseData.success(sysMenuService.getById(id));
    }

    @ApiOperation(value="查询所有菜单")
    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('sys:menu:list')")
    public ResponseData list(){
        List<SysMenu> menus = sysMenuService.tree();
        return ResponseData.success(menus);
    }

    @ApiOperation(value="菜单新增")
    @PostMapping("/save")
    @PreAuthorize("hasAnyAuthority('sys:menu:save')")
    public ResponseData save(@Validated @RequestBody SysMenu sysMenu, Principal principal){
        if(sysMenu.getParentId()==null){
            sysMenu.setParentId("0");
        }
        SysUser sysUser1 = sysUserService.getByUserName(principal.getName());
        if(sysUser1!=null) {
            sysMenu.setCreatedBy(sysUser1.getId());
            sysMenu.setCreatedAt(DateUtils.getYyyyMMddHHmmss(new Date()));
        }

        sysMenuService.save(sysMenu);
        return ResponseData.success(sysMenu);
    }

    @ApiOperation(value="菜单修改")
    @PostMapping("/update")
    @PreAuthorize("hasAnyAuthority('sys:menu:update')")
    public ResponseData update(@Validated @RequestBody SysMenu sysMenu, Principal principal){

        SysUser sysUser1 = sysUserService.getByUserName(principal.getName());
        if(sysUser1!=null) {
            sysMenu.setUpdatedBy(sysUser1.getId());
            sysMenu.setUpdatedAt(DateUtils.getYyyyMMddHHmmss(new Date()));
        }
        sysMenuService.updateById(sysMenu);
        sysUserService.clearUserAuthorityInfoByMenuId(sysMenu.getId());
        return ResponseData.success(sysMenu);
    }

    @ApiOperation(value="删除菜单")
    @PostMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('sys:menu:delete')")
    public ResponseData delete(@PathVariable("id") String id){
        int count = sysMenuService.count(new QueryWrapper<SysMenu>().eq("parent_id", id));

        if(count > 0){
            return ResponseData.fail("请先删除子菜单");
        }

        sysUserService.clearUserAuthorityInfoByMenuId(id);

        sysMenuService.removeById(id);
        sysRoleMenuService.remove(new QueryWrapper<SysRoleMenu>().eq("menu_id", id));

        return ResponseData.success(true);
    }

}
